﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace TpExtensions {
    public static class Extensions {

        #region Members

        public static List<string> TpUser = new List<string> { "As", "Es", "Ew", "Gl", "Gz", "Ks", "Ma", "Mh", "Na", "Nd", "Pk", "Pn", "Rj", "Rk", "Tz", "Wk", "Wl", "Wz", "Zj" };
        public const string tpDateFormat = "yy-MM-dd";
        public const string tpTimeFormat = "HH:mm:ss";
        public const double tpColumnWidth = 66.0;

        public enum tpContent {
            AllJobs = 1,
            ProjectJobs = 2,
            InternJobs = 3
        }
        public enum tpSorting {
            Date = 1,
            Area = 2,
            Hour = 3
        }
        public enum tpSortOrder {
            Up = 1,
            Down = 2
        }
        public enum tpShow {
            Area = 1,
            List = 3
        }


        #endregion

        #region Attributes

        public class StringValueAttribute : Attribute {

            #region Properties

            /// <summary>
            /// Holds the stringvalue for a value in an enum.
            /// </summary>
            public string StringValue { get; protected set; }

            #endregion

            #region Constructor

            /// <summary>
            /// Constructor used to init a StringValue Attribute
            /// </summary>
            /// <param name="value"></param>
            public StringValueAttribute(string value) {
                this.StringValue = value;
            }

            #endregion

        }

        #endregion

        #region Enum

        public static string GetStringValue(this Enum value) {
            // Get the type
            Type type = value.GetType();

            // Get fieldinfo for this type
            FieldInfo fieldInfo = type.GetField(value.ToString());

            // Get the stringvalue attributes
            StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
                typeof(StringValueAttribute), false) as StringValueAttribute[];

            // Return the first if there was a match.
            return attribs.Length > 0 ? attribs[0].StringValue : null;
        }

        #endregion

        #region String

        public static string AddSpace(this string value) {
            return (value == null ? "" : value + " ");
        }

        public static bool IsNumeric(this string value, System.Globalization.NumberStyles NumberStyle) {
            Double result;
            return Double.TryParse(value, NumberStyle,
                System.Globalization.CultureInfo.CurrentCulture, out result);
        }

        public static string Left(this string text, byte length) {
            if(length < 0)
                throw new ArgumentOutOfRangeException("length", length, "length must be > 0");
            else if(length == 0 || text.Length == 0)
                return "";
            else if(text.Length <= length)
                return text;
            else
                return text.Substring(0, length);
        }

        public static string Right(this string original, int numberCharacters) {
            if(original == null | original == String.Empty) {
                return "";
            }
            return original.Substring(numberCharacters > original.Length ? 0 : original.Length - numberCharacters);
        }

        #endregion

        #region Functions

        public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle) {
            Double result;
            return Double.TryParse(val, NumberStyle,
                System.Globalization.CultureInfo.CurrentCulture, out result);
        }

        public static string Left(string text, int length) {
            if(length < 0)
                throw new ArgumentOutOfRangeException("length", length, "length must be > 0");
            else if(length == 0 || text.Length == 0)
                return "";
            else if(text.Length <= length)
                return text;
            else
                return text.Substring(0, length);
        }

        public static string Mid(string param, int startIndex, int length) {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param, int startIndex) {
            //start at the specified index and return all characters after it
            //and assign it to a variable
            string result = param.Substring(startIndex);
            //return the result of the operation
            return result;
        }

        public static T Nz<T>(T Value, T Replacement) {

            try {
                if(Value != null) {
                    if(Value.GetType() != Replacement.GetType()) {
                        if(Replacement.GetType().ToString().Contains("String")) {
                            object ret = Value.ToString();
                            return (T)ret;
                        }
                    }
                    return Value;
                } else {
                    return Replacement;
                }
            } catch(Exception e) {
                Debug.Print(e.ToString());
                return Replacement;
            }

        }

        #endregion
    }
}
